local Alert = require("app.public.Alert")
local TradeUILayer=class("TradeUILayer",FZutil.createUILayer)

function TradeUILayer:ctor()
    self:enableNodeEvents()
	local uimap=FZutil.createSceneNode("TradeUIall/TradeUI")
	self.uimap=uimap
	self:addChild(uimap.root)

    local Button_back = uimap.Button_back

    local function back(e)
        if e.name == "ended" then
            local rclitems = DyTransportPack:getRecycleItems()
            if table.maxn(rclitems) > 0 then
                DyTransportPack:clearRecycleItems()
            end
            local caritems = DyTransportPack:getCartItems()
            if table.maxn(caritems) > 0 then
                DyTransportPack:clearCartItems()      
            end
		    self:removeFromParent()   
        end    
    end
    Button_back:onTouch(back)
    --uimap.root:setPosition(display.center)	

    self:showNpcLayer()

end

function TradeUILayer:showNpcLayer()
    local npcmap=FZutil.createSceneNode("TradeUIall/TradeNpc")
	self.npcmap=npcmap
	self.uimap.root:addChild(npcmap.root)
    npcmap.root:setPosition(display.center)

    local Button_buy = npcmap.Button_buy
    local Button_sale = npcmap.Button_sale

    local function openStore(e)
        if e.name=="ended" then
            local TradeUIStoreLayer = require("app.layers.trade_TradeUIStoreLayer")
            local tradeUIStoreLayer = TradeUIStoreLayer.new()
            self:addChild(tradeUIStoreLayer)

            self.npcmap.root:setVisible(false)
        end
    end
    Button_buy:onTouch(openStore)

    self:UpdateTradeCoins()
end

function TradeUILayer:UpdateTradeCoins()
    self.uimap.Text_goodsinfo1:setString(RoleData.player_info.gold)
    self.uimap.Text_goodsinfo2:setString(RoleData.player_info.diamond)
end


function TradeUILayer:onEnter()
    FzEventCenter:RegisterEvent(FzEvent.UpdateTradeCoins, self, self.UpdateTradeCoins)
end

function TradeUILayer:onExit()
    FzEventCenter:RemoveEvent(FzEvent.UpdateTradeCoins, self)
end

return TradeUILayer